class_name AIBehavior
extends Node

const DURATION_AI_TICK_FREQUENCY := 200



var ball:Ball = null
var opponent_detection_area : Area2D = null
var player : Player = null
var teammate_detection_area : Area2D = null
var time_since_last_ai_tick := Time.get_ticks_msec()


func _ready() -> void:
	time_since_last_ai_tick = Time.get_ticks_msec() + randi_range(0,DURATION_AI_TICK_FREQUENCY)



func setup(context_player:Player,context_ball:Ball,context_opponent_detection_area : Area2D
, context_teammate_detection_area : Area2D) -> void:
	player = context_player
	ball = context_ball
	opponent_detection_area = context_opponent_detection_area
	teammate_detection_area =  context_teammate_detection_area
	


func process_ai() -> void:
	if Time.get_ticks_msec() - time_since_last_ai_tick > DURATION_AI_TICK_FREQUENCY:
		preform_ai_movement()
		perform_ai_decisions()
		time_since_last_ai_tick = Time.get_ticks_msec()
		

func preform_ai_movement() -> void:
	pass

func perform_ai_decisions() -> void:
	pass




#两个同心圆，同心圆不同距离，采用不用的权重参数
func get_bicircular_weight(position:Vector2,center_target:Vector2,
inner_circle_radius:float,inner_circle_weight:float,outer_circle_radius:float,
outer_circle_weight:float) -> float:
	var distance_to_center := position.distance_to(center_target)
	if distance_to_center > outer_circle_radius:
		return outer_circle_weight
	elif distance_to_center<inner_circle_radius:
		return inner_circle_weight
	else:
		var distance_to_inner_radius := distance_to_center - inner_circle_radius
		var close_range_idstance := outer_circle_radius - inner_circle_radius
		return lerpf(inner_circle_weight,outer_circle_weight,distance_to_inner_radius/close_range_idstance)
	




#持球者是对方球员
func is_ball_possessed_by_opponent()->bool:
	return ball.carrier != null and ball.carrier.country != player.country



#判断当前队伍队友是否控球，不同情况下决策不同
func is_ball_carried_by_teammate() -> bool:
	return ball.carrier != null and ball.carrier != player and ball.carrier.country == player.country
	
	
	
func has_opponents_nearby() -> bool:
	var players := opponent_detection_area.get_overlapping_bodies()
	return players.find_custom(func(p:Player): return p.country != player.country) > -1
	
	
	
